﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace SanJ.FreeCare.Authorization.Delegation
{
    [Table("AppUserDelegations")]
    public class UserDelegation : FullAuditedEntity<Guid>, IMultiTenant
    {
        /// <summary>
        /// Id of user who delegates the account
        /// </summary>
        public Guid SourceUserId { get; set; }

        /// <summary>
        /// Id of user who is delegated for the <see cref="SourceUserId"/> account
        /// </summary>
        public Guid TargetUserId { get; set; }

        /// <summary>
        /// TenantId of delegation. Both users must be on same tenant.
        /// </summary>
        public Guid? TenantId { get; set; }

        /// <summary>
        /// Start time of delegation
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// End time of delegation
        /// </summary>
        public DateTime EndTime { get; set; }

        public bool IsCreatedByUser(Guid userId)
        {
            return SourceUserId == userId;
        }

        public bool IsExpired()
        {
            return EndTime <= DateTime.Now;
        }

        public bool IsValid()
        {
            return StartTime <= DateTime.Now && !IsExpired();
        }
    }
}
